package rmhc.gui
{
	import com.adobe.images.PNGEncoder;
	import com.greensock.TweenLite;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import rmhc.RMHCApp;
	import rmhc.api.ApiWrapper;
	import rmhc.api.dataflow.WallPostInfo;
	import rmhc.api.tasks.IGetProfilePicture;
	import rmhc.api.tasks.IPrepareProfileUpload;
	import rmhc.api.tasks.ISetProfilePicture;
	import rmhc.api.tasks.IWallPost;
	import rmhc.design.Screen2Design;
	
	public class AvatarScreen extends Screen2Design
	{
		public var merge:MergeManager;
		public var anim:DomikAnim;
		
		protected var ap:ApiWrapper;
		
		protected var pict:IGetProfilePicture;
		protected var prep:IPrepareProfileUpload;
		protected var prof:ISetProfilePicture;
		protected var publ:IWallPost;
		
		protected var avaBytes:ByteArray;
		protected var prep1:int = 0;
		
		public function AvatarScreen()
		{
			super();
			btnSave.mouseEnabled = false;
			ap = RMHCApp.getApi();
			pict = ap.getProfilePicture( null );
			pict.addEventListener( Event.COMPLETE , onGetPic );
			pict.run();
			
			prep = ap.prepareProfileUpload();
			prep.addEventListener( Event.COMPLETE , onPrepProf );
			prep.run();
			
			btnSave.addEventListener( MouseEvent.CLICK , onSaveAva );
			btnPubl.addEventListener( MouseEvent.CLICK , onPublAva );
			btnPubl.mouseEnabled = btnSave.mouseEnabled = false;
		}
		
		protected function onGetPic( e:Event ):void
		{
			var r:Rectangle = RESIZE_AREA;
			merge = new MergeManager( pict.getPicture() , RMHCApp.getServerUrl() + "ladoshka.png" , r.width , r.height );
			merge.alpha = 0;
			merge.x = r.x;
			merge.y = r.y;
			addChild( merge );
			TweenLite.to( merge , 1 , { alpha : 1 } );
			onPrep1();
		}
		protected function onPrepProf( e:Event ):void
		{
			onPrep1();
		}
		protected function onPrep1():void
		{
			if ( ++prep1 >= 2 )
			{
				btnSave.mouseEnabled = true;
			}
		}
		protected function onSaveAva( e:MouseEvent ):void
		{
			trace( "onSaveAva" );
			btnSave.mouseEnabled = false;
			//merge.mouseEnabled = merge.mouseChildren = false;
			var m:BitmapData = merge.getMerged();
			prof = ap.setProfilePicture( m );
			prof.addEventListener( Event.COMPLETE , onSetProfile );
			prof.addEventListener( IOErrorEvent.NETWORK_ERROR , onRefuseProfile );
			prof.run();
			
			if ( anim != null )
			{
				removeChild( anim );
			}
			anim = new DomikAnim();
			anim.x = domik.x;
			anim.y = domik.y;
			addChild( anim );
			anim.setBitmap( merge );
			
			//TEST
			//onSetProfile( null );
		}
		protected function onSetProfile( e:Event ):void
		{
			trace( "onSetProfile" );
			domik.visible = false;
			//показываем домик!
			
			anim.addEventListener(Event.COMPLETE , onAnimDone );
			anim.playAnim();
			//merge.visible = false;
		}
		protected function onAnimDone( e:Event ):void
		{
			setState( PUBLISH );
			btnPubl.mouseEnabled = true;
		}
		protected function onRefuseProfile( e:Event ):void
		{
			trace( "onRefuseProfile" );
			anim.revert();
			btnSave.mouseEnabled = true;
		}
		
		protected function onPublAva( e:MouseEvent ):void
		{
			btnPubl.mouseEnabled = false;
			//ap.pre
			//avaBytes = PNGEncoder.encode( merge.getElement() );
		/*}
		protected function onProfilePrepared( e:Event ):void
		{*/
			//var post:WallPostInfo = new WallPostInfo( ap.getUID() , "Я поставил ладошку на аватар" , "Помоги картошке, поставь ладошку!" , WallPostInfo.MEDIA );
			var post:WallPostInfo = new WallPostInfo( ap.getUID() , "Я поставил ладошку на аватар!" , "Поставь ладошку на аватар!" , WallPostInfo.MEDIA );
			post.link = "http://rmhc.ru";
			post.mediaId = ap.getLadoshkId();
			//post.thumbPng = avaBytes;
			publ = ap.postToWall( post );
			publ.addEventListener( Event.COMPLETE , onPublDone );
			publ.run();
		}
		protected function onPublDone( e:Event ):void
		{
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}